import csv

import jionlp


rcsv = open('data/data.csv', 'r', encoding="utf-8")
reader = csv.reader(rcsv)
data = list(reader)
result = []
for i in range(1, len(data)):
    address = data[i][8]
    location = jionlp.parse_location(address)
    if location.get('city') is None:
        # 如果 'city' 键的值为 None，执行相应的操作
        location = jionlp.parse_location(data[i][1])
        location['Id'] = i
        location['JobId'] = data[i][0]
        location['company'] = data[i][1]
        result.append(location)
    else:
        location['Id'] = i
        location['JobId'] = data[i][0]
        location['company'] = data[i][1]
        result.append(location)

# 定义 CSV 文件路径
csv_file = 'data/city.csv'

# 定义 CSV 文件的表头
header = ['Id', 'JobId', 'company', 'Province', 'City', 'County']

# 将结果写入 CSV 文件
with open(csv_file, 'w', newline='', encoding='utf-8') as file:
    writer = csv.writer(file)

    # 写入表头
    writer.writerow(header)

    # 写入数据行
    for row in result:
        writer.writerow([
            row.get('Id', '未知'),
            row.get('JobId', '未知'),
            row.get('company', '未知'),
            row.get('province', '未知'),
            row.get('city', '未知'),
            row.get('county', '未知'),
        ])

print("CSV 文件保存成功！")
